﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Apollo;
using NHibernate;

namespace Apollo.Website
{
    public partial class ShareUserView : System.Web.UI.Page
    {
        protected UserView userView;

        protected void Page_Load(object sender, EventArgs e)
        {
            int userViewId = 0;

            if (int.TryParse(this.Request["UserViewId"], out userViewId))
            {
                try
                {
                    userView = Apollo.Application.CurrentSession.QueryOver<UserView>()
                        .Where(uv => uv.Id == userViewId)
                        .SingleOrDefault();

                    LabelEntityName.Text = userView.Name;

                    if (!this.Page.IsPostBack)
                    {
                        PopulateDropDown();
                    }
                }
                catch (Exception ex)
                {
                    MessageList.AddMessage(new ActionMessage("Share failed: " + ex.Message, ActionMessageType.Error));
                }
            }
            else
            {
                MessageList.AddMessage(new ActionMessage("No UserView found!", ActionMessageType.Error));
            }
        }

        protected void PopulateDropDown()
        {
            DropDownListUserList.DataTextField = "Name";
            DropDownListUserList.DataValueField = "Id";

            DropDownListUserList.DataSource = Apollo.Application.CurrentSession.QueryOver<User>()
                .Where(u => u.Id != userView.User.Id)
                .OrderBy(u => u.Name)
                .Asc
                .List();
            DropDownListUserList.DataBind();
        }

        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            ITransaction tran = Apollo.Application.CurrentSession.BeginTransaction();

            try
            {
                UserView uv = new UserView()
                {
                    Entity = userView.Entity,
                    Filter = userView.Filter,
                    MenuItem = userView.MenuItem,
                    Name = userView.Name,
                    OrderBy = userView.OrderBy,
                    OrderDirection = userView.OrderDirection,
                    ViewString = userView.ViewString,
                    User = Apollo.Application.CurrentSession.QueryOver<User>().Where(u => u.Id == int.Parse(DropDownListUserList.SelectedValue)).SingleOrDefault()
                };

                Apollo.Application.CurrentSession.Save(uv);

                tran.Commit();

                MessageList.AddMessage(new ActionMessage("Shared with : " + DropDownListUserList.SelectedItem.Text, ActionMessageType.Info));
            }
            catch (Exception ex)
            {
                tran.Rollback();

                MessageList.AddMessage(new ActionMessage("Share failed: " + ex.Message, ActionMessageType.Error));
            }
        }
    }
}